package com.example.number1.ui.theme;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.number1.R;

public class dialogActivity extends AppCompatActivity {

    private Button button;

    private AlertDialog dialog;
    private ProgressBar progressBar;
    private int width;
    private int height;


    private TextView textView;

    private int progressStatus = 0;
    private Handler handler = new Handler();
    private int i=100;
    private int i1=50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        // 点击按钮弹出对话框
        findViewById(R.id.btnDialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    private void showDialog() {
        // 加载自定义布局就是弹出的对话框
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);

        //进度条要加载多少的组件
        EditText edittext= view.findViewById(R.id.editText);
        //进度条加载的时间的组件
        EditText editTime= view.findViewById(R.id.editTime);



        // 创建对话框构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override// 处理确定按钮点击事件
                    public void onClick(DialogInterface dialog, int which) {
                        //显示进度条
                        progressBar.setVisibility(View.VISIBLE);
                        //销毁对话框
                        dismissDialog();

                        String s = edittext.getText().toString();
                        if (s != "" && !s.isEmpty()) {
                            i = Integer.parseInt(s.trim());
                        }

                        String s1 = editTime.getText().toString();

                        if (s1 != "" && s1.isEmpty()) {
                            i1 = Integer.parseInt(s1.trim());
                        }
                        //获取进度条的组件
                        progressBar = findViewById(R.id.progressBar);
                        //获取进度条的执行情况
                        textView = findViewById(R.id.textView);

                        new Thread(new Runnable() {
                            public void run() {
                                while (progressStatus < i) {
                                    progressStatus += 1;

                                    handler.post(new Runnable() {
                                        public void run() {
                                            //填充内容
                                            progressBar.setProgress(progressStatus);
                                            //记录填充的进度
                                            textView.setText("正在加载: " + progressStatus + "%");
                                        }
                                    });

                                    try {
                                        Thread.sleep(i1); // 模拟耗时操作
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }

                                handler.post(new Runnable() {
                                    public void run() {
                                        textView.setText("加载完成！");

                                      //  Intent intent = new Intent(getApplicationContext(), Image.class);
                                      //  startActivity(intent);


                                    }
                                });
                            }
                        }).start();

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override// 处理取消按钮点击事件
                    public void onClick(DialogInterface dialog, int which) {

                        progressBar.setVisibility(View.VISIBLE);
                        dismissDialog();
                    }
                });

        // 创建对话框实例
        dialog = builder.create();

        // 设置对话框动画
        Window window = dialog.getWindow();
        if (window != null) {
            startProgressBarAnimation();
            //设置对话框动画
            window.setWindowAnimations(R.style.DialogAnimation);
        }
        // 显示对话框
        dialog.show();
    }

    private void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
    private void startProgressBarAnimation() {
        // 获取进度条控件的引用
        progressBar = findViewById(R.id.progressBar);

        width = progressBar.getWidth();
        height = progressBar.getHeight();

        // 设置位移动画：从原始位置（0, 0）向下位移200像素
        ObjectAnimator translateY = ObjectAnimator.ofFloat(progressBar, "translationY", 0, 300);

        startWidthHeightAnimation1(progressBar,935,540);


        // 设置动画移动持续时间
        translateY.setDuration(600);


        // 创建组合动画
        AnimatorSet animatorSet = new AnimatorSet();

        // 按顺序播放位移和缩放动画
        animatorSet.playSequentially(translateY);

        // 添加动画监听器，在动画结束时执行逆向的位移动画
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                // 创建逆向的位移动画：从当前位置向上位移200像素
                ObjectAnimator reverseTranslateY = ObjectAnimator.ofFloat(progressBar, "translationY", 200, 0);
                //宽高都返回原来的大小
                startWidthHeightAnimation1(progressBar,width,height);
                //设置进度条为隐藏
                progressBar.setVisibility(View.GONE);
                //逆向动画时间
                reverseTranslateY.setDuration(500);
                // 启动逆向的位移动画
                reverseTranslateY.start();
            }
        });

        // 启动动画
        animatorSet.start();
    }

    private void startWidthHeightAnimation1(View view, int targetWidth, int targetHeight) {
        // 创建ValueAnimator，设置起始值和目标值
        ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), targetWidth);
        ValueAnimator heightAnimator = ValueAnimator.ofInt(view.getHeight(), targetHeight);

        // 设置动画监听器
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 在动画更新时更新View的宽度
                int value = (int) animation.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.width = value;
                view.setLayoutParams(layoutParams);
            }
        });

        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 在动画更新时更新View的高度
                int value = (int) animation.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });

        // 设置动画缩放持续时间
        widthAnimator.setDuration(500);
        heightAnimator.setDuration(500);

        // 启动动画
        widthAnimator.start();
        heightAnimator.start();
    }



 /*   private void startProgressBarAnimation() {
        // 获取进度条控件的引用
        ProgressBar progressBar = findViewById(R.id.progressBar);

        // 设置位移动画：从原始位置（0, 0）向下位移200像素
        ObjectAnimator translateY = ObjectAnimator.ofFloat(progressBar, "translationY", 0, 200);

        // 设置缩放动画：从原始尺寸（100%）缩放到目标尺寸（50%），再恢复到原始尺寸
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(progressBar, "scaleX", 1f, 2f, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(progressBar, "scaleY", 1f, 2f, 1f);

        // 设置动画持续时间
        translateY.setDuration(500);
        scaleX.setDuration(500);
        scaleY.setDuration(500);

        // 创建组合动画
        AnimatorSet animatorSet = new AnimatorSet();

        // 按顺序播放位移和缩放动画
        animatorSet.playSequentially(translateY, scaleX, scaleY);

        // 添加动画监听器，在动画结束时执行逆向的位移动画
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                // 创建逆向的位移动画：从当前位置向上位移200像素
                ObjectAnimator reverseTranslateY = ObjectAnimator.ofFloat(progressBar, "translationY", 200, 0);
                reverseTranslateY.setDuration(500);

                // 启动逆向的位移动画
                reverseTranslateY.start();
            }
        });

        // 启动动画
        animatorSet.start();
    }*/



   /*     button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 加载自定义布局
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_layout, null);

                // 创建对话框，并设置自定义布局
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setView(view);

                // 获取输入框的引用
                EditText editText = view.findViewById(R.id.edit_text);

                // 设置确定按钮和取消按钮的点击事件
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String inputText = editText.getText().toString();
                        // 在此处处理确定按钮点击事件和输入框的内容
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 在此处处理取消按钮点击事件
                    }
                });

                // 创建对话框实例
                AlertDialog dialog = builder.create();

                // 设置对话框弹出动画
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;

                // 显示对话框
                dialog.show();
            }
        });*/
    }
